//
//  ImagePickerViewController.m
//  PlayApple
//
//  Created by programmer on 16/2/18.
//  Copyright © 2016年 中电华神. All rights reserved.
//

#import "ImagePickerViewController.h"

@interface ImagePickerViewController ()

@end

@implementation ImagePickerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor grayColor];
    
    [self configUI];
}

- (void)configUI{
    
    self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 64, kScreenWidth, kScreenHeight -64)];
    [self.view addSubview:self.imageView];
    

    UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
    button1.frame = CGRectMake(80, 200, kScreenWidth-160, 55);
    [button1 setTitle:@"从照相机中抓取" forState:UIControlStateNormal];
    [button1 addTarget:self action:@selector(pickPhotoCamera:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button1];
    
    UIButton *button2 = [UIButton buttonWithType:UIButtonTypeCustom];
    button2.frame = CGRectMake(80, 300, kScreenWidth-160, 55);
    [button2 setTitle:@"从图片库中抓取" forState:UIControlStateNormal];
    [button2 addTarget:self action:@selector(pickPhotoLibrary:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button2];
    
}

// 从照相机中抓取
- (void)pickPhotoCamera:(id)sender {

    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
        if (_imagePicker == nil) {
            _imagePicker = [[UIImagePickerController alloc] init];
        }
        _imagePicker.delegate = self;
        _imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
        
        [self presentViewController:_imagePicker animated:YES completion:nil];
    } else {
        NSLog(@"照相机不可用");
    }
}

// 从图片库中抓取
- (void)pickPhotoLibrary:(id)sender {

    if (_imagePicker == nil) {
        _imagePicker = [[UIImagePickerController alloc] init];
    }
    _imagePicker.delegate = self;
    _imagePicker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
//    _imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    
    [self presentViewController:_imagePicker animated:YES completion:nil];
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {

    _imagePicker.delegate = nil;
    
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info {

    /**
     从参数info中取出原始图片数据，参数info如果抓取的是图片，则包含了原始或编辑后的图片数据，如果抓取的是视屏，则包含的是视屏存放路径。。
     UIImagePickerControllerOriginalImage键是获取原始图片数据，此外常用的键还有
     UIImagePickerControllerMediaType       由用户指定的媒体类型
     UIImagePickerControllerOriginalImage   获取原始图片数据
     UIImagePickerControllerEditedImage     编辑后的图片数据
     UIImagePickerControllerCropRect        裁减后的图片数据
     UIImagePickerControllerMediaURL        视屏存放路径
     */
    
    UIImage *originalImage = (UIImage *) [info objectForKey:UIImagePickerControllerOriginalImage];
    
    self.imageView.image = originalImage;
    self.imageView.contentMode = UIViewContentModeScaleAspectFill;
    
    _imagePicker.delegate = nil;
    
    [self dismissViewControllerAnimated:YES completion:nil];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
